package com.itheima.tlias.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.annotation.Log;
import com.itheima.tlias.dto.ReportGenderCountDTO;
import com.itheima.tlias.dto.SelectEmpDTO;
import com.itheima.tlias.dto.SelectEmpDetailDTO;
import com.itheima.tlias.entity.Exe;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.mapper.ExeMapper;
import com.itheima.tlias.po.InsertEmpParam;
import com.itheima.tlias.po.SelectEmpPageParam;
import com.itheima.tlias.po.UpdateEmpParam;
import com.itheima.tlias.service.EmpService;
import com.itheima.tlias.util.ResultUtil;
import com.itheima.tlias.vo.PageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    EmpMapper empMapper;

    @Resource
    ExeMapper exeMapper;

    @Override
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        //分页插件
        List<SelectEmpDTO> dto = empMapper.selectEmpByPage(param);
        PageVO<SelectEmpDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        //返回总页面的数量
        pageVO.setRows(dto);
        //返回当前页面的数据
        return ResultUtil.success(pageVO);
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDelete(List<Integer> ids) {
        //先删除员工信息
        int flag = empMapper.batchDelete(ids);
        //再删除员工履历
        int i = exeMapper.batchDeleteExe(ids);
        log.info("删除了{}条旧履历",i);
        return flag;
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEmp(InsertEmpParam param) {
        int flag = empMapper.insertEmp(param);
        //插入工作履历
        log.info("员工回显的主键{}",param.getId());
        List<Exe> exprList = param.getExprList();
        if(CollectionUtils.isNotEmpty(exprList)){
            //赋值更新时间
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exeMapper.batchInsertExe(exprList);
            log.info("插入员工履历{}",i);
        }

        return flag;
    }

    @Override
    public SelectEmpDetailDTO selectEmpById(Integer id) {
        return empMapper.selectEmpById(id);
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(UpdateEmpParam param) {
        param.setUpdateTime(LocalDateTime.now());
        //先更新员工表信息
        int empRows = empMapper.updateEmp(param);
        //2.再删除该员工的所有履历
        int exeDelRows = exeMapper.batchDeleteExe(List.of(param.getId()));
        log.info("删除{}条旧履历数据",exeDelRows);
        //3.新增所有履历
        List<Exe> exprList = param.getExprList();
        if(CollectionUtils.isNotEmpty(exprList)){
            //赋值更新时间
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exeMapper.batchInsertExe(exprList);
        }
        return empRows;
    }

    @Override
    public List<ReportGenderCountDTO> empGenderData() {
        return empMapper.empGenderData();
    }

    @Override
    public ResultUtil empJobData() {
        List<Integer> jobs = empMapper.empJobData();
        Map<String, List<Integer>> map = jobs.stream().collect(Collectors.groupingBy(t -> {
            return switch (t) {
                case 1 -> "班主任";
                case 2 -> "讲师";
                case 3 -> "学工主管";
                case 4 -> "教研主管";
                case 5 -> "咨询师";
                default -> "其他";
            };
        }));
        Set<String> jobList = map.keySet();
        List<Integer> dataList = map.values().stream().map(List::size).toList();
//        HashMap<String, Object> resultMap = new HashMap<>();
//        resultMap.put("jobList",jobList);
//        resultMap.put("dataList",dataList);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("jobList",jobList);
        jsonObject.put("dataList",dataList);
        return ResultUtil.success(jsonObject);
    }


    @Override
    public List<SelectEmpDTO> selectAllMaster() {
        return empMapper.selectAllMaster();
    }
}
